<?php
/**
 * Created by PhpStorm.
 * User: Taylor
 * Date: 2018/6/19
 * Time: 14:42
 */

namespace app\index\controller;

use think\Controller;
use think\Cookie;
use think\Db;
use think\Request;
use think\Session;

class Login extends Controller
{

    public function login(Request $request)
    {
        $data = $request->post();
        $pass = md5($data['pass']);
        $res = Db('user')->where('tel',$data['tel'])->where('password',$pass)->find();
        $logindata['name']= $res['name'];
        $logindata['id']= $res['id'];
        $logindata['icon']= $res['icon'];
        if($res)
        {
            if (!empty($data['checkbox']))
            {   //todo:如果勾选了记住on就设置7天的cookie;
                // Cookie::set('logincookie',$logindata,'604800');
                Cookie::set('loginname',$res['name'],'604800');
                Cookie::set('loginicon',$res['icon'],'604800');
                Cookie::set('loginid',$res['id'],'604800');
            }else{
                Session::set('user',$logindata);
            }

                $this->redirect('index/Index/index');

        }else
            {
                $this->error('你的账户或者密码输入有误');
            }

    }

    public function logout()
    {
        Session::delete('user','think');
        Cookie::delete('loginname');
        Cookie::delete('loginicon');
        Cookie::delete('loginid');

        $this->redirect('index/Index/index');

    }
}